/*
 * name: common
 * version: 0.3.3
 * update: date过滤器分钟显示bug
 * date: 2015-11-11
 */
 
define(function(require, exports, module) {
	"use strict";
	
	//ajax错误处理
	window.catchAjaxError = function(code, status) {
		appcan.loading.hide();
		$('#scrollLoadSpinning').hide();
		switch (code) {
			case 0:
				appcan.window.openToast('网络错误，请检查网络连接！', 3000, 5, 0);
				// if (!appcan.ls.val('notice')) {
				// 	appcan.ls.val('notice', '网络错误，请检查网络连接！');
				// 	setTimeout(function() {
				// 		appcan.openView(null, 'common', 'notice');
				// 	}, 0);
				// }
				break;
			case 1:
				appcan.window.openToast('请求异常中断！', 3000, 5, 0);

				break;
			case 2:
				appcan.window.openToast('数据接收错误！', 3000, 5, 0);

				break;
			case 3:
				appcan.window.openToast('数据解析错误！', 3000, 5, 0);

				break;
			default://4
				appcan.window.openToast('服务端错误(code:' + status + ')', 3000, 5, 0);

				break;
		}
	};
	$.ajaxSetup({
		timeout: appcfg.set.outime,
		beforeSend: function(o, setting) {
			appcan.device.getInfo(13, function(err, data, dataType, optId) {
				if (!err) {
					if (data.split) {
						data = JSON.parse(data);
					}
					if (data.connectStatus == '-1') {
						o.abort();
					}
				}
			});
		},
		error: function(o) {
			catchAjaxError(o.readyState, o.status);
		}
	});
	//浮动窗口扩展selfTop
	if(appcan.ls.val('popoverTopHash')){
		window.selfTop = appcan.ls.val('popoverTopHash');
		appcan.ls.remove('popoverTopHash');
	}else{
		window.selfTop = 0;
	};
	//返回按钮
	$('#goBack').on('click',function(){
		appcan.window.close();
	});
	//主栏目跳转
	$('#home').on('click', function() {
		appcan.openView({
			anim: 0
		}, "home");
	});
	$('#service').on('click', function() {
		appcan.openView({
			anim: 0
		}, "service");
	});
	$('#parts').on('click', function() {
		appcan.openView({
			anim: 0
		}, "parts");
	});
	$('#cart').on('click', function() {
		appcan.openView({
			anim: 0
		}, "shop", "shopCart");
	});
	$('#member').on('click', function() {
		appcan.openView({
			anim: 0
		}, "member");
	});

	//批量绑定openView
	$('body').on('click', '.openView', function(e) {
		e.preventDefault();
		var $this = $(this),
			v = $this.data('view');
		$this.removeClass('reddot').find('.reddot').removeClass('reddot');
		if (v) {
			v = v.split(',');
			$this.addClass('active');
			setTimeout(function(){
				$this.removeClass('active');
				appcan.openView({
					anim: v[0]
				}, v[1], v[2]);
				$this = v = null;
			},100);
		}
	});

	//弹出编辑
	require('popEditer');
	$('body').on('click', '.editBle', function() {
		var $textitem = $(this).find('.edit-text');
		if ($textitem.length) {
			var placeholder = $textitem.text(),
				extDate = {};
			if ($textitem.attr('checktype')) {
				extDate.datatype = $textitem.attr('checktype');
			}
			if ($textitem.hasClass('item-body')) {
				extDate.tag = "textarea";
			}
			$.popEditer(placeholder, function(newVal) {
				if (placeholder != newVal) {
					$textitem.text(newVal).addClass('changed');
				}
			}, extDate);
		}

	});

    //安卓兼容
	var androidCompa = function(version) {
		//item-radio强制重绘
		if (parseFloat(version) < 4.3) {
			$('body').on('click', '.item-radio', function() {
				var $view = $(this).parent();
				$view.css('visibility', 'hidden');
				setTimeout(function() {
					$view.css('visibility', 'visible');
				}, 0);
			});
		};

		//三星note2
		if (/GT-N710\d/.test(navigator.userAgent)) {
			$('body').css('font-size',parseInt($('body').css('font-size'))/1.5);
		}
	};
	//苹果兼容
	var iosCompa = function(version){
		if(parseFloat(version)>=9){
			$('body').on('click', '.item-radio', function() {
				var $view = $(this).parent();
				$view.css('visibility', 'hidden');
				setTimeout(function() {
					$view.css('visibility', 'visible');
				}, 0);
			});
		}
	
	};

	//图片点击放大
	$('body').on('click','img.ableOpenImg',function(){
		var imgs = $('body').find('img.ableOpenImg'),
			src = $(this).attr('src'),
			index,
			imgsArr = [];
		$.each(imgs,function(i,e){
			if($(e).attr('src')){
				if($(e).attr('src')==src){
					index = i;
				}
				imgsArr.push($(e).attr('src'));
			}
		});
		if(window.uexImageBrowser){
			uexImageBrowser.open(imgsArr,index);
		}else{
			window.open(src);
		}
		src = imgsArr = imgs = index = null;
	});
	//输入状态
	$('body').on('focus','input',function(){
		$('body').addClass('onKeyboard');
		$('.keyboardHide').hide();
	}).on('blur','input',function(){
		$('body').removeClass('onKeyboard');
		$('.keyboardHide').show();
	});

	appcan.ready(function() {
		//隐藏滚动条
		uexWindow.setWindowScrollbarVisible(false);
		//系统兼容
		if (platform == 'android') {
			androidCompa(version);
		}
		if (platform == 'ios') {
			iosCompa(version);
		};

		//新版本状态提醒
		appcan.window.subscribe('newVersion',function(version){
			if(version){
				
			}
		});

		//前后台状态
		appcan.window.on('pause',function(){
			window.isBack = true;
		});
		appcan.window.on('resume',function(){
			window.isBack = false;
		});
		//响应关闭指令
		appcan.window.subscribe('closeback', function(msg) {
			switch(msg){
				case "closeByNew"://被openView的新页面关闭
					if(window.closeByNew){
						appcan.window.close();
					}
					break;
				case "closeback"://关闭后台页面
					if(window.isBack){
						appcan.window.close();
					}
					break;
			}
		});
		//发布关闭指令
		var closeCondition = appcan.ls.val('winCloseCondition');
		appcan.window.publish('closeback', closeCondition);
		if(closeCondition){
			appcan.ls.remove('winCloseCondition');
			closeCondition = null;
		};
		//监听网络状态
		appcan.window.subscribe('NET',function(net){
			if(net=='off'){
				appcan.window.openToast('网络链接中断！',2000,8);
			}
		});


	});
});